package com.test.testapp.app;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class KNMI extends Activity {


        private Button loadButton;
        private static List<String> list = new ArrayList<String>();
        private static String line;
        private ArrayAdapter <String> adapter;
        private ProgressDialog barProgressDialog;
        private DatePicker datePicker;
        private String date;
        private Dialog dialog;
        private String bewolkt = null;
        private String neerslag = null;
        private boolean gevonden = false;


        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_knmi);
            loadButton = (Button) findViewById(R.id.loadButton);
            datePicker = (DatePicker) findViewById(R.id.datePicker);
            datePicker.setCalendarViewShown(false);
            setListeners();
        }

    private void setListeners()
    {
        loadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadMyData();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.knmi, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        return super.onOptionsItemSelected(item);
    }

    public void run(View v)
    {
        //onClick();
    }


    InputStream str = new InputStream()
    {
        @Override
        public int read() throws IOException {
            return 0;
        }
    };

    public void loadMyData()
    {
        barProgressDialog = new ProgressDialog(KNMI.this);
        barProgressDialog.setTitle("Gegevens aan het laden ...");
        barProgressDialog.setMessage("Graag even geduld ...");
        barProgressDialog.setProgressStyle(barProgressDialog.STYLE_SPINNER);
        barProgressDialog.setProgress(0);
        barProgressDialog.setMax(20);

        loadScreenShow();
        int year = datePicker.getYear();
        int month = datePicker.getMonth();
        int day = datePicker.getDayOfMonth();
        String  monthString = null;
        String dayString = null;
        month ++;
        if (month < 10)
        {
            monthString = "0"+String.valueOf(month);
        }
        else
        {
            monthString = String.valueOf(month);
        }
        if (day < 10)
        {
            dayString = "0"+String.valueOf(day);
        }
        else
        {
            dayString = String.valueOf(day);
        }
        date = String.valueOf(year)+monthString+dayString;
        System.out.println(date);

        Log.w("historischeDataApp", "WORKS!");

        //new Thread(new Runnable() {
        //public void run() {
        try
        {
            //InputStream in = this.getAssets().open("raw/weerdata.txt");
            InputStream in = getClass().getResourceAsStream("raw/weerdata.txt");
            Log.w("historischeDataApp", "WORKS!!");
            //BufferedReader br = new BufferedReader( new InputStreamReader(in));
            BufferedReader br;
            InputStreamReader fr = new InputStreamReader(getResources().openRawResource(R.raw.weerdata));
            br = new BufferedReader(fr);
            Log.w("historischeDataApp", "WORKS!!!");
            while((line = br.readLine())!= null)
            {
                if(line.contains("#") == false)
                {
                    list.add(line);
                }
            }
            br.close();
            int index = 0;
            for (String s : list)
            {
                int i = 0;
                String [] splitData = s.split(",");
                do
                {
                    //String to int
                    //int tempData = Integer.parseInt(splitData[i]);
                    // String splitDate = splitData[1];
                    if (date.equals(splitData[1]))
                    {
                        //do check on date
                        Log.w("historischeDataApp", "splitData[i]");
                        System.out.println(splitData[1]);
                        gevonden = true;
                        neerslag = splitData[8];
                        bewolkt = splitData[6];
                        neerslag = neerslag.replaceAll("\\s+","");
                        System.out.println(neerslag);
                        bewolkt = bewolkt.replaceAll("\\s+","");
                        int bewolking = Integer.parseInt(bewolkt);
                        switch (bewolking)
                        {
                            case 1: bewolkt = "geen bewolking";
                                break;
                            case 2:
                            case 3:
                            case 4:bewolkt = "minimaal bewolkt";
                                break;
                            case 5:
                            case 6:bewolkt = "matig bewolkt";
                                break;
                            case 7:
                            case 8:
                            case 9:bewolkt = "zwaar bewolkt";
                                break;
                        }
                    }
                    i++;
                }
                while (!(splitData.length <= i || gevonden));
            }
            //while(!(list.size() <= index || gevonden));
        }
        catch (IOException error)
        {
            System.out.println("IOException");
            System.out.println(error.getCause());
            System.out.println(error.getMessage());
        }
        catch (NumberFormatException e)
        {
            System.out.println("Format of the string is incorrect");
        }
        loadScreenHide();
        //}}).start();
        if (gevonden = true)
        {
            dialog = new Dialog(this);
            dialog.setTitle(dayString + "-" + monthString + "-" + year);
            TextView weerData = new TextView(this);
            weerData.setText("Op deze dag was het " + bewolkt + " en " + neerslag + " milimeter neerslag.");
            weerData.setGravity(Gravity.CENTER_HORIZONTAL);
            dialog.setContentView(weerData);
            dialog.show();
        }
        else
        {
            dialog = new Dialog(this);
            dialog.setTitle("Geen data van: " + dayString + "-" + monthString + "-" + year);
            TextView weerData = new TextView(this);
            weerData.setText("Er is geen data van " + date + " gevonden. Probeer een andere datum.");
            weerData.setGravity(Gravity.CENTER_HORIZONTAL);
            dialog.setContentView(weerData);
            dialog.show();
        }
    }
    public void loadScreenShow()
    {
        barProgressDialog.show();
    }
    public void loadScreenHide()
    {
        barProgressDialog.hide();
    }

}
